{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用2014年整个9月份数据作为测试数据，用来验证模型效果。编写Python将数据p的特征进行转换，并对全体数据进行标准化处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import pickle\n",
    "import keras\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "\n",
    "data = pd.read_csv(\"data/p.csv\")\n",
    "parts = 14\n",
    "this_one = data.iloc[parts:]\n",
    "bak_index = this_one.index\n",
    "for k in range(1, parts + 1):\n",
    "    last_one = data.iloc[(parts - k):(this_one.shape[0] - k + parts)]\n",
    "    this_one.set_index(last_one.index, drop=True, inplace=True)\n",
    "    this_one = this_one.join(last_one, lsuffix=\"\", rsuffix=\"_p\" + str(k))\n",
    "\n",
    "this_one.set_index(bak_index, drop=True, inplace=True)\n",
    "this_one = this_one.fillna(0)\n",
    "t0 = this_one.iloc[:, 0:96]\n",
    "t0_min = t0.apply(lambda x: np.min(x), axis=0).values\n",
    "t0_ptp = t0.apply(lambda x: np.ptp(x), axis=0).values\n",
    "this_one = this_one.apply(lambda x: (x - np.min(x)) / np.ptp(x), axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这里对负荷数据的标准化参数进行存储，后继对预测数据进行量纲还原。进一步对数据进行分区，得到训练数据和测试数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_data = this_one.iloc[-30:]\n",
    "train_data = this_one.iloc[:-30]\n",
    "train_y_df = train_data.iloc[:, 0:96]\n",
    "train_y = np.array(train_y_df)\n",
    "train_x_df = train_data.iloc[:, 96:]\n",
    "train_x = np.array(train_x_df)\n",
    "\n",
    "test_y_df = test_data.iloc[:, 0:96]\n",
    "test_y = np.array(test_y_df)\n",
    "test_x_df = test_data.iloc[:, 96:]\n",
    "test_x = np.array(test_x_df)\n",
    "test_y_real = t0.iloc[-30:]\n",
    "\n",
    "with open('data/train_x.pkl', 'wb') as f:\n",
    "    pickle.dump(train_x, f)\n",
    "    \n",
    "with open('data/train_y.pkl', 'wb') as f:\n",
    "    pickle.dump(train_y, f)\n",
    "    \n",
    "with open('data/test_x.pkl', 'wb') as f:\n",
    "    pickle.dump(test_x, f)\n",
    "    \n",
    "with open('data/test_y_real.pkl', 'wb') as f:\n",
    "    pickle.dump(test_y_real, f)\n",
    "    \n",
    "with open('data/t0_ptp.pkl', 'wb') as f:\n",
    "    pickle.dump(t0_ptp, f)\n",
    "\n",
    "with open('data/t0_min.pkl', 'wb') as f:\n",
    "    pickle.dump(t0_min, f)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
